package com.bdmc.car.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.bdmc.common.core.page.TableDataInfo;
import com.bdmc.common.core.domain.PageQuery;
import org.springframework.stereotype.Service;
import com.bdmc.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bdmc.common.utils.StringUtils;
import com.bdmc.car.domain.bo.CarGarageBo;
import com.bdmc.car.domain.vo.CarGarageVo;
import com.bdmc.car.domain.CarGarage;
import com.bdmc.car.mapper.CarGarageMapper;
import com.bdmc.car.service.ICarGarageService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 修车厂Service业务层处理
 *
 * @author bdmc
 * @date 2023-01-06
 */
@Service
public class CarGarageServiceImpl extends ServicePlusImpl<CarGarageMapper, CarGarage, CarGarageVo> implements ICarGarageService {

    @Override
    public CarGarageVo queryById(Integer garageId){
        return getVoById(garageId);
    }

    @Override
    public TableDataInfo<CarGarageVo> queryPageList(CarGarageBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CarGarage> lqw = buildQueryWrapper(bo);
        Page<CarGarageVo> result = pageVo(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<CarGarageVo> mySelectPage(CarGarageBo bo, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.mySelectPage(pageQuery.build(),bo));
    }

    @Override
    public List<CarGarageVo> queryList(CarGarageBo bo) {
        LambdaQueryWrapper<CarGarage> lqw = buildQueryWrapper(bo);
        return listVo(lqw);
    }

    private LambdaQueryWrapper<CarGarage> buildQueryWrapper(CarGarageBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CarGarage> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeptId() != null, CarGarage::getDeptId, bo.getDeptId());
        lqw.like(StringUtils.isNotBlank(bo.getGarageName()), CarGarage::getGarageName, bo.getGarageName());
        lqw.eq(StringUtils.isNotBlank(bo.getGarageAddr()), CarGarage::getGarageAddr, bo.getGarageAddr());
        lqw.eq(StringUtils.isNotBlank(bo.getManager()), CarGarage::getManager, bo.getManager());
        lqw.eq(StringUtils.isNotBlank(bo.getMobile()), CarGarage::getMobile, bo.getMobile());
        lqw.eq(bo.getEnabled()!=null, CarGarage::getEnabled, bo.getEnabled());
        return lqw;
    }

    @Override
    public Boolean insertByBo(CarGarageBo bo) {
        CarGarage add = BeanUtil.toBean(bo, CarGarage.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            bo.setGarageId(add.getGarageId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(CarGarageBo bo) {
        CarGarage update = BeanUtil.toBean(bo, CarGarage.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(CarGarage entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
